package com.doge.config;

import com.alibaba.druid.util.StringUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @program: doge
 * @description: 通过请求头传递 sessionId
 * @author: Keyu Li
 * @create: 2021-12-01 19:45
 **/


public class MarketSessionManager extends DefaultWebSessionManager {

    private static final String ADMIN_TOKEN_HEADER = "X-CskaoyanMarket-Admin-Token";
    private static final String WX_TOKEN_HEADER = "X-CskaoyanMarket-Token";

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        HttpServletRequest req = (HttpServletRequest) request;
        String headerAdmin = req.getHeader(ADMIN_TOKEN_HEADER);
        if (!StringUtils.isEmpty(headerAdmin)){
            return headerAdmin;
        }
        String headerWx = req.getHeader(WX_TOKEN_HEADER);
        if (!StringUtils.isEmpty(headerWx)){
            return headerWx;
        }
        // 如果没有包含，则 新建 session
        return super.getSessionId(req, response);
    }
}